var app = getApp()

Page({

  data: {
    sysWidth:0,
    sysHeight:0,

    isShow: true,

    yuanshiUrl: '',
    imgUrl: '',
    bgImgUrl: '',
    width: 0,
    height: 0,
    flags: {},
    
    zuopingName: ''
  },

  onLoad: function(options) {
    var that = this
    // console.log(options);
    wx.uploadFile({
      url: app.globalData.url + 'upload_img',
      filePath: options.imgUrl,
      name: 'file',
      success: (res)=>{
        // console.log(res);
        if(res.data){
          that.setData({
            yuanshiUrl: res.data
          })
        }
        // console.log(res);
      }
    });

    this.setData({
      imgUrl: options.imgUrl
    })
    this.getConfig()

    if(!app.globalData.userInfo){
      app.globalData.userInfo = wx.getStorageSync('MagicPlateUserInfo');
      that.setData({
        zuopingName: app.globalData.userInfo.nickName + ' 的大作'
      })
    }

    wx.getSystemInfo({
      success: (res)=>{
        that.setData({
          sysWidth: res.windowWidth,
          sysHeight: res.windowHeight
        })
      },
      fail: ()=>{},
      complete: ()=>{}
    });
  },

  getConfig: function() {
    var that = this
    wx.request({
      url: app.globalData.url + 'config/v/' + app.globalData.version,
      success: function(res) {
        that.setData({
          bgImgUrl: res.data.msg,
          flags: res.data.data
        })
        
        that.drawCanvas()
      }
    })
  },

  drawCanvas: function() {
    var that = this
    // 获取屏幕宽高
    var sysInfo = wx.getSystemInfoSync()

    var canvas = wx.createCanvasContext('canvas')
    canvas.setFillStyle('#fff')
    canvas.fillRect(0, 0, 300, 800)
    wx.getImageInfo({
      src: that.data.bgImgUrl,
      success: function(res) {
        that.setData({
          width: res.width,
          height: res.height,
        })
        var bgWidth = res.width
        canvas.drawImage(res.path, 0, 0, res.width, res.height)
        wx.getImageInfo({
          src: that.data.imgUrl,
          success: function(res) {
            canvas.drawImage(res.path, bgWidth * 0.174, bgWidth * 0.35, bgWidth * 0.65, bgWidth * 0.65)
            canvas.draw()
            that.setT()
          }
        })
      }
    })
  },

  setT: function() {
    var that = this
    setTimeout(function() {
      wx.canvasToTempFilePath({
        canvasId: 'canvas',
        success: function(res) {
          that.setData({
            imgUrl: res.tempFilePath,
          })
          wx.hideLoading()
        },
        fail: function(res) {
          wx.hideLoading()
          // console.log(res)
        }
      })
    }, 300)
  },

  previewImg: function(e) {
    var img = this.data.imgUrl
    wx.previewImage({
      current: img,
      urls: [img]
    })
  },

  onShareAppMessage: function() {
    return {
      title: '创作属于你的艺术!',
      imageUrl: '/images/share.jpg',
      path: '/pages/index/index'
    }
  },

  qimingCancel: function() {
    var that = this
    if(that.data.yuanshiUrl){
      wx.request({
        url: app.globalData.url + 'add_item',
        data: {
          userid: app.globalData.userId,
          url: that.data.yuanshiUrl,
          name: app.globalData.userInfo.nickName + ' 的大作'
        },
        method: 'POST',
        success: (res) => {
          that.setData({
            isShow: false
          })
        }
      });
    }
    
  },
  qimingSave: function() {
    var that = this
    if(that.data.yuanshiUrl){
      wx.request({
        url: app.globalData.url + 'add_item',
        data: {
          userid: app.globalData.userId,
          url: that.data.yuanshiUrl,
          name: that.data.zuopingName
        },
        method: 'POST',
        success: (res) => {
          that.setData({
            isShow: false
          })
        }
      });
    }
    
  },
  getName: function(e) {
    this.setData({
      zuopingName: e.detail.value.replace(/^\s*|\s*$/g , "")
    });
  },

  save: function() {
    var that = this;
    wx.showLoading({
      mask: true,
      title: "保存中"  
    })
    wx.saveImageToPhotosAlbum({      
      filePath: that.data.imgUrl,
      success: (res) => {
        console.log('saveImageToPhotosAlbum success');
        
        wx.hideLoading()
        wx.showModal({
          title: "保存成功",
          content: "图片已经保存到手机相册，将图片分享给朋友吧~",
          showCancel: false
        })
      },
      fail: function(error) {
        console.log('saveImageToPhotosAlbum fail');
        console.log(error);
        
        wx.hideLoading();
        wx.openSetting({
          success(settingdata) {
            console.log(settingdata)
            if (settingdata.authSetting['scope.writePhotosAlbum']) {
              console.log('获取权限成功，给出再次点击图片保存到相册的提示。')
            } else {
              console.log('获取权限失败，给出不给权限就无法正常使用的提示')
            }
          }
        })
      }
    })
  },
})